package com.jacobson.core.widget.security

import com.jacobson.core.widget.security.authc.AuthenticationInfo
import com.jacobson.core.widget.security.authc.AuthenticationToken

/**
 * ================================================
 *
 *  @author Wucz on 2018/8/7 8:52
 * ================================================
 */
class DefaultSubjectContext : SubjectContext {

    override var authenticated: Boolean = false
    override var authenticationToken: AuthenticationToken? = null
    override var authenticationInfo: AuthenticationInfo? = null
    override var securityManager: SecurityManager? = null
    override var subject: Subject? = null
    override var principal: Any? = null

    constructor()
    constructor(context: SubjectContext?) : this() {
        context?.let {
            authenticated = it.authenticated
            authenticationInfo = it.authenticationInfo
            authenticationToken = it.authenticationToken
            subject = it.subject
        }
    }

    override fun resolveSecurityManager(): SecurityManager? = securityManager


    override fun resolveSession(): Any? = null

    override fun resolveAuthenticated(): Boolean {
        return authenticated || authenticationInfo != null
    }

    override fun resolvePrincipal(): Any? {
        var principalTemp: Any? = principal
        principalTemp = principalTemp ?: authenticationInfo?.principal
        return principalTemp ?: subject?.principal
    }
}